﻿using NCP.Core;
using NCP.Core.Exceptions;
using NCP.Model;
using NCP.Service;
using NCP.Util;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
using WebApp.Filters;

namespace WebApp.Controllers
{
    public class GroupChoseDrugCompareController : Controller
    {
        private GroupChoseDrugCompareService _service;

        public GroupChoseDrugCompareController(GroupChoseDrugCompareService service)
        {
            _service = service;
        }
        // GET: DeptMergeInfo
        public ActionResult Index()
        {
            List<GroupPurchase> list = _service.GroupPurchaseSelect();
            ViewData["gplist"] = list;
            return View();
        }
      

        //非中选药品对照
        public ActionResult ChoseDrugCompare(string gpCode,string drugcode,string  drugname,string specs,string enabled)
        {
            ViewData["gpcode"] = gpCode;
            ViewData["drugcode"] = drugcode;
            ViewData["drugname"] = drugname;
            ViewData["specs"] = specs;
            ViewData["enabled"] = enabled;
            return View();
        }
        //非中选药品对照
        public ActionResult GroupVarietyDrugCompare(string gpCode, string drugcode,string drugname,string specs, string enabled)
        {
            ViewData["gpcode"] = gpCode;
            ViewData["drugcode"] = drugcode;
            ViewData["drugname"] = drugname;
            ViewData["specs"] = specs;
            ViewData["enabled"] = enabled;
            return View();
        }
        //获取是否正在执行的批次 下拉菜单
        public ActionResult GetGroupInfo(string param)
        {
            List<GroupPurchase> list = _service.GroupPurchaseSelectByParam(param);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        //获取某个批次中选药品信息 下拉菜单
        public ActionResult GetChoseDrugInfo(string gpCode)
        {
            List<PhaBaseInfo> list = _service.GetChoseDrugInfo(gpCode);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        //获取非中选药品信息 
        public ActionResult GetunChoseDrugInfo()
        {
            string gpCode = Request["GpCode"];
            string drugcode= Request["drugCode"];
            List <PhaBaseInfo> dataList = _service.GetunChoseDrugInfo(gpCode, drugcode);
            LayData ht = new LayData("0", "获取成功", dataList);
            return Content(JsonConvert.SerializeObject(ht));
        }
        //获取同品种药品信息 
        public ActionResult GetVarietyDrugInfo()
        {
            string gpCode = Request["GpCode"];
            string drugcode = Request["drugCode"];
            List<PhaBaseInfo> dataList = _service.GetVarietyDrugInfo(gpCode, drugcode);
            LayData ht = new LayData("0", "获取成功", dataList);
            return Content(JsonConvert.SerializeObject(ht));
        }

        //获取已维护非中选药品
        public ActionResult GetVarietydrugCompares()
        {
            string gpCode = Request["GpCode"];
            string drugCode= Request["drugCode"];
            List<GroupVarietyDrugCompareNew> dataList = _service.GetVarietydrugCompares(gpCode, drugCode);
            LayData ht = new LayData("0", "获取成功", dataList);
            return Content(JsonConvert.SerializeObject(ht));
        }

        //获取已维护同品种药品
        public ActionResult GetUnChosedrugCompares()
        {
            string gpCode = Request["GpCode"];
            string drugCode = Request["drugCode"];
            List<GroupChoseDrugCompareNew> dataList = _service.GetUnChosedrugCompares(gpCode, drugCode);
            LayData ht = new LayData("0", "获取成功", dataList);
            return Content(JsonConvert.SerializeObject(ht));
        }
        //获取某个批次中选药品信息 表格数据
        public ActionResult GetList(string gpCode, string drugCode,string isSet)
        {
            Expressionable<GroupChoseDrug> exp = new Expressionable<GroupChoseDrug>();
            int pageIndex = 1;
            int pageSize = 20;
            if (!string.IsNullOrEmpty(Request.Form["page"]))
            {
                pageIndex = Convert.ToInt32(Request.Form["page"]);
            }
            if (!string.IsNullOrEmpty(Request.Form["limit"]))
            {
                pageSize = Convert.ToInt32(Request.Form["limit"]);
            }
            PageModel pageModel = new PageModel() { PageIndex = pageIndex, PageSize = pageSize };
            List<GroupChoseDrug> dataList = _service.GetPageList(gpCode, drugCode, isSet, pageModel);
            LayData ht = new LayData("0", "获取成功", dataList, pageModel.TotalCount);

            return Content(JsonConvert.SerializeObject(ht));
        }

        public ActionResult SaveUnchoseDrug(List<GroupChoseDrugCompareNew> items, string gpCode,string drugCode)
        {
            LayData data = new LayData("0", "保存成功");
            try
            {
                _service.SaveUnchoseDrug(items, gpCode,drugCode);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }
        public ActionResult SaveVarietyDrug(List<GroupVarietyDrugCompareNew> items, string gpCode, string drugCode)
        {
            LayData data = new LayData("0", "保存成功");
            try
            {
                _service.SaveVarietyDrug(items, gpCode, drugCode);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }
        //上传非中选药品对照信息
        public ActionResult ImportUnchoseDrug(string gpCode)
        {

            LayData data = new LayData("0", "导入成功");
            try
            {
                if (gpCode == "" || gpCode==null)
                {
                    data = new LayData("-1", "请选择集采批次");
                    return Json(data);
                }
                var files = Request.Files;

                if (files.Count > 0)
                {
                    var filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "upload", files[0].FileName);
                    if (files[0].ContentLength > 0)
                    {
                        files[0].SaveAs(filePath);
                        DataTable dt = ExcelHelper.ExcelToDataTable(filePath);
                        if (_service.ImportUnchoseDrug(dt, gpCode) == -1)
                        {
                            data.code = "-1";
                            data.msg = "上传文件无内容";
                            return Json(data);
                        }
                    }
                }
                else
                {
                    data.code = "-1";
                    data.msg = "上传文件无内容";
                }
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }
        //上传同品种药品对照信息
        public ActionResult ImportVarietyDrug(string gpCode)
        {
            LayData data = new LayData("0", "导入成功");
            try
            {
                var files = Request.Files;

                if (files.Count > 0)
                {
                    var filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "upload", files[0].FileName);
                    if (files[0].ContentLength > 0)
                    {
                        files[0].SaveAs(filePath);
                        DataTable dt = ExcelHelper.ExcelToDataTable(filePath);
                        _service.ImportVarietyDrug(dt, gpCode);
                    }
                }
                else
                {
                    data.code = "2";
                    data.msg = "无上传文件";
                }
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }
        public ActionResult ImportTempletFzx()
        {
            DataTable dt = _service.ImportTempletFzx();
            string filename = ExcelHelper.ExportToExcel(dt, "非中选药品维护模板");
            return File(filename, "application/vnd.ms-excel", "非中选药品维护模板.xls");
        }

        public ActionResult ImportTempletTpz()
        {
            DataTable dt = _service.ImportTempletFzx();
            string filename = ExcelHelper.ExportToExcel(dt, "同品种药品维护模板");
            return File(filename, "application/vnd.ms-excel", "同品种药品维护模板.xls");
        }
        //导出维护数据
        public ActionResult export(string gpcode)
        {
            DataTable dt = _service.export(gpcode);
            string filename = ExcelHelper.ExportToExcel(dt, "非中选药品对照数据");
            return File(filename, "application/vnd.ms-excel", "非中选药品对照数据.xls");
        }
    }
}